﻿using _07_Panel_嵌入式窗体.窗体;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _07_Panel_嵌入式窗体
{
    public partial class MainForm : BaseForm
    {
        public MainForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 在指定的Panel中显示窗体
        /// </summary>
        /// <param name="controlForm">窗体</param>
        /// <param name="panel">panel容器</param>
        public void ShowForm(Form controlForm, Panel panel)
        {
            foreach (Control item in panel.Controls)
            {
                if (item is Form fr) //判断容器中如果已经有窗体，那么关闭
                {
                    fr.Close();
                }
            }
            //设置为非顶级窗体(顶级窗体是可以在Wim10中自由移动的窗体，但是目前我需要将窗体嵌入到容器中因此设置为非顶级窗体)
            controlForm.TopLevel = false;
            //设置窗体的边框样式=>无边框
            controlForm.FormBorderStyle = FormBorderStyle.None;
            //设置窗体填充满panel容器
            controlForm.Dock = DockStyle.Fill;
            //将窗体添加到panel容器中
            panel.Controls.Add(controlForm);
            //显示窗体
            controlForm.Show();
        }

        private void label2_Click(object sender, EventArgs e)
        {
            SetLabelActive(sender);

            ShowForm(new Frm_Main(), this.panel3);
        }

        private void label3_Click(object sender, EventArgs e)
        {
            SetLabelActive(sender);

            ShowForm(new Frm_System(), this.panel3);
        }

        private void label4_Click(object sender, EventArgs e)
        {
            SetLabelActive(sender);

            ShowForm(new Frm_License(), this.panel3);
        }

        private void label5_Click(object sender, EventArgs e)
        {
            SetLabelActive(sender);
            ShowForm(new Frm_Helper(), this.panel3);
        }

        private void label6_Click(object sender, EventArgs e)
        {
            SetLabelActive(sender);
            ShowForm(new Frm_LogSystem(), this.panel3);
        }

        private void SetLabelActive(object activeLabel)
        {
            foreach (Control item in this.panel2.Controls)
            {
                if (item is Label label)
                {
                    if (label.Text == ((Label)activeLabel).Text)
                    {
                        label.BackColor = Color.Orange;
                    }
                    else
                    {
                        label.BackColor = Color.SeaGreen;
                    }
                }
            }
        }



        /// <summary>
        /// 标记是否展开
        /// </summary>
        bool flag;

        /// <summary>
        /// 隐藏/显示 侧边栏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label7_Click(object sender, EventArgs e)
        {
            this.timer1.Start(); //启动定时器
            if (this.panel2.Width < 193)
            {
                //展开
                flag = true;
            }
            else
            {
                //收缩
                flag = false;
            }
        }
        /// <summary>
        /// 定时器响应时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //判断是展开还是搜索
            if (flag)
            {
                this.panel2.Width += 10;
                if (this.panel2.Width >= 193)
                {
                    this.timer1.Stop();
                    this.panel2.Width = 193;
                }
                //设置箭头图标
                this.label7.Image = Properties.Resources.箭头_左_白;

            }
            else
            {
                this.panel2.Width -= 10;
                if (this.panel2.Width <= 45)
                {
                    this.timer1.Stop();
                    this.panel2.Width = 45;
                }
                //设置箭头图标
                this.label7.Image = Properties.Resources.箭头_右_白;
            }

        }
    }
}
